<template>
  <div class="bg">
    <my-login center :login="loginHandle" @success="handleSuccess" title="Joseph Center">
      <div slot="footer" class="clearfix">
         
          <span  style="float:left"> 密码提示： j(J)oseph / 123456</span>
          <el-link type="text" style="float:right" @click="goPreview">预览</el-link>
         
      </div>
    </my-login>
    <Wave width="100%" height="100%"></Wave>
  </div>
</template>

<script>

  import {LOGIN} from '$my/code/api/user' // login, 
  import axios from 'axios'
  import {encryptByDES} from '$ui/utils/crypto'
  export default {
    components: {
      Wave: () => import('$ui/components/my-wave')
    },
    props: {
      // url查询参数名称
      urlQueryName: {
        type: String,
        default: 'url'
      },
      // 默认登录成功跳转页面地址
      defaultUrl: {
        type: String,
        default: '/member'
      }
    },
    methods: {
      loginApi(account, password) {
        return axios({
          method: 'post',
          url: LOGIN,
          data: {
            userName: encryptByDES(account, 'JOSEPH_CENTER'),
            password: encryptByDES(password, 'JOSEPH_CENTER') 
          }
        }).then((res) => { 
          if (res.data.data && res.data.code === 0) {
            return Promise.resolve(res.data.data)
          } else {
            return Promise.reject(res.data)
          }
        }).catch((err) => { 
          return Promise.reject(err)
        })
      },
      loginHandle({account, password}) { 
        return this.loginApi(account, password)
      },
      handleSuccess(res) {
        // 在此记录登录信息和跳转
        if (!this.$access) return
        this.$access.login({
          ...res.userInfo,
          token: res.token
        })
        this.redirect()
      },
      isSelf(url) {
        const {fullPath, path} = this.$route
        return url === fullPath || url === path || url === window.location.href
      },
      redirect() {
        const redirectUrl = this.$route.query[this.urlQueryName] || this.defaultUrl
        const path = decodeURIComponent(redirectUrl)
        // 如果准备要跳转的地址就是页面本身，不需要处理
        if (this.isSelf(path)) return
        const regex = /^http(s)?:\/\/.+/
        if (regex.test(path)) {
          window.location.href = path
        } else {
          this.$router.push(path)
        }
      },
      goPreview() {
        this.$router.push({path: '/preview'})
      }
    },
    created() {
      // 如果已经是登录状态，跳转到首页或指定页面
      if (this.$access && this.$access.isLogin()) {
        this.redirect()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .bg {
    position: relative;
    width: 100%;
    height: 100vh;
    background: url("~$ui/assets/bg/01.jpg") center center;
  }

  .my-login {
    z-index: 3;
  }

  .my-wave {
    position: absolute;
    left: 0;
    bottom: 0;
  }
</style>
